﻿<script lang="ts" name="storage" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { Plus } from "@element-plus/icons-vue";
import { UploadRequestOptions } from "element-plus";
import { useStorageApi } from '/@/api/InboundManagementssrk/storage';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const storageApi = useStorageApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
});

// 页面加载时
onMounted(async () => {
  const data = await storageApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.inboundId = data.inboundId ?? [];
  state.dropdownData.incomingOrdersId = data.incomingOrdersId ?? [];
  state.dropdownData.vendor = data.vendor ?? [];
  state.dropdownData.organizationalStructureId = data.organizationalStructureId ?? [];
  state.dropdownData.attn = data.attn ?? [];
  state.dropdownData.formsPerson = data.formsPerson ?? [];
  state.dropdownData.department = data.department ?? [];
  state.dropdownData.reviewers = data.reviewers ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await storageApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await storageApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

const uploadUploadAttachmentsHandle = async (options: UploadRequestOptions) => {
	const res = await storageApi.uploadUploadAttachments(options);
	state.ruleForm.uploadAttachments = res.data.result?.url;
	};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="storage-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库单号" prop="storageCode">
							<el-input v-model="state.ruleForm.storageCode" placeholder="请输入入库单号" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库类型" prop="storageIdType">
							<el-input v-model="state.ruleForm.storageIdType" placeholder="请输入入库类型" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库状态" prop="storageStatus">
							<el-input v-model="state.ruleForm.storageStatus" placeholder="请输入入库状态" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="仓库名称" prop="inboundId">
							<el-select clearable filterable v-model="state.ruleForm.inboundId" placeholder="请选择仓库名称">
								<el-option v-for="(item,index) in state.dropdownData.inboundId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库日期" prop="storageDate">
							<el-date-picker v-model="state.ruleForm.storageDate" type="date" placeholder="入库日期" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进货订单编号" prop="incomingOrdersId">
							<el-select clearable filterable v-model="state.ruleForm.incomingOrdersId" placeholder="请选择进货订单编号">
								<el-option v-for="(item,index) in state.dropdownData.incomingOrdersId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库数量" prop="storageNumber">
							<el-input-number v-model="state.ruleForm.storageNumber" placeholder="请输入入库数量" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库金额" prop="storageMoney">
							<el-input-number v-model="state.ruleForm.storageMoney" placeholder="请输入入库金额" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商" prop="vendor">
							<el-select clearable filterable v-model="state.ruleForm.vendor" placeholder="请选择供应商">
								<el-option v-for="(item,index) in state.dropdownData.vendor" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系人" prop="contact">
							<el-input v-model="state.ruleForm.contact" placeholder="请输入联系人" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系方式" prop="contactNumber">
							<el-input v-model="state.ruleForm.contactNumber" placeholder="请输入联系方式" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="组织机构" prop="organizationalStructureId">
							<el-select clearable filterable v-model="state.ruleForm.organizationalStructureId" placeholder="请选择组织机构">
								<el-option v-for="(item,index) in state.dropdownData.organizationalStructureId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="经办人" prop="attn">
							<el-select clearable filterable v-model="state.ruleForm.attn" placeholder="请选择经办人">
								<el-option v-for="(item,index) in state.dropdownData.attn" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="storageDesc">
							<el-input v-model="state.ruleForm.storageDesc" placeholder="请输入备注" maxlength="32" type="textarea" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="上传附件" prop="uploadAttachments">
							<el-upload
								list-type="picture-card"
								:show-file-list="false"
								:http-request="uploadUploadAttachmentsHandle">
								<img
									v-if="state.ruleForm.uploadAttachments"
									:src="state.ruleForm.uploadAttachments"
									@click="state.ruleForm.uploadAttachments=''"
									style="width: 100%; height: 100%; object-fit: contain"/>
									<el-icon v-else><Plus /></el-icon>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="制单人" prop="formsPerson">
							<el-select clearable filterable v-model="state.ruleForm.formsPerson" placeholder="请选择制单人">
								<el-option v-for="(item,index) in state.dropdownData.formsPerson" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="制单时间" prop="formsTime">
							<el-date-picker v-model="state.ruleForm.formsTime" type="date" placeholder="制单时间" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="所在部门" prop="department">
							<el-select clearable filterable v-model="state.ruleForm.department" placeholder="请选择所在部门">
								<el-option v-for="(item,index) in state.dropdownData.department" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="审核人" prop="reviewers">
							<el-select clearable filterable v-model="state.ruleForm.reviewers" placeholder="请选择审核人">
								<el-option v-for="(item,index) in state.dropdownData.reviewers" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="审核时间" prop="reviewTime">
							<el-date-picker v-model="state.ruleForm.reviewTime" type="date" placeholder="审核时间" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>